macro(pythonize_bool var)
  if (${var})
    set(${var} True)
  else()
    set(${var} False)
  endif()
endmacro()

set(LIBCUDACXX_HIGHEST_COMPUTE_ARCH 90)

foreach (COMPUTE_ARCH ${LIBCUDACXX_COMPUTE_ARCHS})
  set(_compute_message "${_compute_message} sm_${COMPUTE_ARCH}")
  set(LIBCUDACXX_COMPUTE_ARCHS_STRING "${LIBCUDACXX_COMPUTE_ARCHS_STRING} ${COMPUTE_ARCH}")
endforeach ()

option(LIBCUDACXX_ENABLE_COMPUTE_FUTURE "Enable code generation for tests for compute_${LIBCUDACXX_HIGHEST_COMPUTE_ARCH}" OFF)
if (LIBCUDACXX_ENABLE_COMPUTE_FUTURE)
  set(_compute_message "${_compute_message} compute_${LIBCUDACXX_HIGHEST_COMPUTE_ARCH}")
endif ()

message(STATUS "Enabled CUDA architectures:${_compute_message}")

option(LIBCUDACXX_TEST_WITH_NVRTC
  "Test libcu++ with runtime compilation instead of offline compilation. Only runs device side tests."
  OFF)

if (LIBCUDACXX_TEST_WITH_NVRTC)
  if (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
    message(FATAL_ERROR "NVRTC testing not supported on Windows. \
      Set LIBCUDACXX_TEST_WITH_NVRTC to OFF.")
  endif()

  set(LIBCUDACXX_CUDA_COMPILER "${CMAKE_CURRENT_SOURCE_DIR}/../utils/nvidia/nvrtc/nvrtc.sh")
  set(LIBCUDACXX_CUDA_COMPILER_ARG1 "${CMAKE_CUDA_COMPILER}")
else() # NOT LIBCUDACXX_TEST_WITH_NVRTC
  set(LIBCUDACXX_FORCE_INCLUDE "-include ${CMAKE_CURRENT_SOURCE_DIR}/force_include.h")
  set(LIBCUDACXX_CUDA_COMPILER "${CMAKE_CUDA_COMPILER}")
endif()

if (NOT MSVC)
  set(LIBCUDACXX_WARNING_LEVEL
    "--compiler-options=-Wall \
     --compiler-options=-Wextra")
endif()

# Intel OneAPI compiler has fast math enabled by default which breaks almost all floating point tests
if (${CMAKE_CXX_COMPILER_ID} STREQUAL "IntelLLVM")
  set(LIBCUDACXX_TEST_COMPILER_FLAGS
    "${LIBCUDACXX_TEST_COMPILER_FLAGS} \
    --compiler-options=-fno-fast-math")
endif()

if (${CMAKE_CUDA_COMPILER_ID} STREQUAL "NVIDIA")
  set(LIBCUDACXX_TEST_COMPILER_FLAGS
    "${LIBCUDACXX_TEST_COMPILER_FLAGS} \
    ${LIBCUDACXX_FORCE_INCLUDE} \
    -I${CMAKE_SOURCE_DIR}/include \
    ${LIBCUDACXX_WARNING_LEVEL}")
endif()

if (${CMAKE_CUDA_COMPILER_ID} STREQUAL "NVCXX")
  set(LIBCUDACXX_TEST_COMPILER_FLAGS
    "${LIBCUDACXX_TEST_COMPILER_FLAGS} \
    -stdpar")
  set(LIBCUDACXX_TEST_LINKER_FLAGS
    "${LIBCUDACXX_TEST_LINKER_FLAGS} \
    -stdpar")
endif()

include(AddLLVM)

set(LIBCUDACXX_TARGET_INFO "libcudacxx.test.target_info.LocalTI" CACHE STRING
"TargetInfo to use when setting up test environment.")
set(LIBCUDACXX_EXECUTOR "None" CACHE STRING
"Executor to use when running tests.")

# NVRTC needs extra time until we can get compilation seperated.
if (LIBCUDACXX_TEST_WITH_NVRTC)
  set(LIBCUDACXX_TEST_TIMEOUT "0" CACHE STRING "Enable test timeouts (Default = 200, Off = 0)")
else()
  set(LIBCUDACXX_TEST_TIMEOUT "200" CACHE STRING "Enable test timeouts (Default = 200, Off = 0)")
endif()

set(AUTO_GEN_COMMENT "## Autogenerated by libcudacxx configuration.\n# Do not edit!")

pythonize_bool(LIBCUDACXX_ENABLE_COMPUTE_FUTURE)

configure_lit_site_cfg(
  ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
  ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg)

add_lit_testsuite(check-cudacxx
  "Running libcu++ tests"
  ${CMAKE_CURRENT_BINARY_DIR})

# Add test target for standalone headers
add_subdirectory(host_only)
